package com.zyz.K_集合.Map接口三TreeMap;

import java.util.Objects;

/**
 * @author: vick
 * @Date: 2025/3/30
 * @Description:
 * @version: 1.0
 */
public class User implements Comparable{
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return age == user.age && Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public int compareTo(Object o) {
        if (this == o) {
            return 0;
        }

        //按照age 从小到大排序，如果age相同，按照name从大到小排序
        if (o instanceof User) {
            User user = (User) o;
            int value = this.age - user.age;
            if (value != 0) {
                return value;
            }
            return -this.name.compareTo(user.getName());
        }
        throw new RuntimeException("输入的类型不匹配");
    }
}
